package com.nc.delivery_project.client.orders;

import com.google.gwt.cell.client.EditTextCell;
import com.google.gwt.cell.client.TextCell;
import com.google.gwt.user.cellview.client.Column;
import com.google.gwt.user.cellview.client.ColumnSortEvent;
import com.google.gwt.user.cellview.client.DataGrid;
import com.nc.delivery_project.client.composite.PagingDataGrid;
import com.nc.delivery_project.shared.proxy.OrderProxy;

/**
 * Created with IntelliJ IDEA.
 * User: Natalie D.
 * Date: 1/17/15
 * Time: 10:13 PM
 */
public class OrderPagingDataGrid extends PagingDataGrid<OrderProxy> {

    @Override
    public void initTableColumns(DataGrid<OrderProxy> dataGrid, ColumnSortEvent.ListHandler<OrderProxy> sortHandler) {
        Column<OrderProxy, String> idCol = createIdCol();
        Column<OrderProxy, String> senderCol = createSenderCol();
        Column<OrderProxy, String> receiverCol = createReceiverCol();
        Column<OrderProxy, String> startDateCol = createStartDateCol();
        Column<OrderProxy, String> deliveryDateCol = createDeliveryDateCol();
        Column<OrderProxy, String> commentCol = createCommentCol();
        Column<OrderProxy, String> statusCol = createStatusCol();
        Column<OrderProxy, String> paymentCol = createPaymentCol();
        dataGrid.addColumn(idCol, "ID");
        dataGrid.addColumn(senderCol, "Sender");
        dataGrid.addColumn(receiverCol, "Receiver");
        dataGrid.addColumn(startDateCol, "Start Date");
        dataGrid.addColumn(deliveryDateCol, "Delivery Date");
        dataGrid.addColumn(commentCol, "Comment");
        dataGrid.addColumn(statusCol, "Status");
        dataGrid.addColumn(paymentCol, "Payment");
    }
    private Column<OrderProxy, String> createPaymentCol() {
        Column<OrderProxy, String> col = new Column<OrderProxy, String>(new TextCell()) {
            @Override
            public String getValue(OrderProxy object) {
                if (object.getPayment()!=null)
                return String.valueOf(object.getPayment().getAmount());
                return "no payment set";
            }
        };

        return col;
    }
    private Column<OrderProxy, String> createStatusCol() {
        Column<OrderProxy, String> col = new Column<OrderProxy, String>(new TextCell()) {
            @Override
            public String getValue(OrderProxy object) {
                return object.getStatus();
            }
        };

        return col;
    }
    private Column<OrderProxy, String> createCommentCol() {
        Column<OrderProxy, String> col = new Column<OrderProxy, String>(new TextCell()) {
            @Override
            public String getValue(OrderProxy object) {
                return object.getComment();
            }
        };

        return col;
    }
    private Column<OrderProxy, String> createSenderCol() {
        Column<OrderProxy, String> col = new Column<OrderProxy, String>(new TextCell()) {
            @Override
            public String getValue(OrderProxy object) {
                return object.getSender().getEmail();
            }
        };

        return col;
    }
    private Column<OrderProxy, String> createDeliveryDateCol() {
        Column<OrderProxy, String> col = new Column<OrderProxy, String>(new TextCell()) {
            @Override
            public String getValue(OrderProxy object) {
                if (object.getDeliveryDate()!=null)
                return object.getDeliveryDate().toString();
                return "no date";
            }
        };

        return col;
    }
    private Column<OrderProxy, String> createStartDateCol() {
        Column<OrderProxy, String> col = new Column<OrderProxy, String>(new TextCell()) {
            @Override
            public String getValue(OrderProxy object) {
                if (object.getStartDate()!=null)
                return object.getStartDate().toString();
                return "no date";
            }
        };

        return col;
    }

    private Column<OrderProxy, String> createReceiverCol() {
        Column<OrderProxy, String> col = new Column<OrderProxy, String>(new EditTextCell()) {
            @Override
            public String getValue(OrderProxy object) {
                return object.getReceiver().getEmail();
            }
        };

        return col;
    }

    private Column<OrderProxy, String> createIdCol() {
        Column<OrderProxy, String> col = new Column<OrderProxy, String>(new TextCell()) {
            @Override
            public String getValue(OrderProxy object) {
                return String.valueOf(object.getId());
            }
        };

        return col;
    }
}
